<?php

namespace App\Http\Controllers\Back;

use App\Models\Back\__MODEL__;
use App\Tool\Tool;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class __CONTROLLER__Controller extends Controller
{
	/*
	 * 设置
	 * get请求显示页面
	 * post请求添加品牌或修改品牌信息
	 */
	public function set (Request $request, $id = 0)
	{
		if ($request->isMethod('post')) {
			//实例化模型
			$Model = new __MODEL__();
			//表单验证
			$Model->setValidator($request->all());

			//更新数据库
			$res = $Model->set($request->all());

			if ($res) {
				return route('__WEB__.list');
			} else {
				return back()->withInput();
			}
		} else {
			if ($id) {
				//获取单条品牌信息
				$__VIEW__ = __MODEL__::find($id);
				return view('back.__VIEW__.set', ['__VIEW__' => $__VIEW__]);
			} else {
				return view('back.__VIEW__.set');
			}
		}
	}



	/*
	 * 展示品牌列表
	 * post提交过来为删除操作
	 */
	public function show (Request $request, $field = 'id', $order = 'asc')
	{
		$Model = new __MODEL__();
		if ($request->isMethod('post')) {
			//验证
			$Model->deleteValidator($request->all());
			//删除
			$Model->delete__MODEL__($request->input('selected'));
			return back();
		} else {
			//取排序后的结果集
			$Collection = $Model->__VIEW__Order($field, $order);
			//处理页面class和url问题
			$list = Tool::sortClass($field, $order);

			$list['__VIEW__Collection'] = $Collection;
			return view('back.__VIEW__.list', $list);
		}
	}
}
